﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using TaobaoDistrSales.Common;
using TaobaoDistrSales.Model;

namespace TaobaoDistrSales
{
    public partial class ShopForm : DockContent
    {
        #region Ctor
        public ShopForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Load

        private void ShopForm_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        #endregion

        #region Add Delete Save

        private void AddButton_Click(object sender, EventArgs e)
        {
            try
            {
                taobaodbEntities entities = ResourceManager.Current.LoadEntity();
                string appKey = this.AppKeyTextBox.Text.Trim();
                string appSecret = AppSecretTextBox.Text.Trim();
                var shop = (from s in entities.Shop
                            where s.AppKey == appKey && s.AppSecret == appSecret
                            select s).FirstOrDefault();
                if (shop != null)
                {
                    MessageBox.Show("已经存在AppKey:" + appKey + ", AppSecret:" + appSecret + "的店铺\n,请重新输入AppKey和AppSecret!");
                }
                else
                {
                    shop = new Shop();
                    shop.Name = ShopNameTextBox.Text.Trim();
                    shop.AppKey = appKey;
                    shop.AppSecret = appSecret;
                    entities.AddToShop(shop);
                    entities.SaveChanges();
                    BindGrid();
                }
            }
            catch { }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            DataGridViewRow currentRow = MainDataGridView.CurrentRow;
            if (currentRow != null && !currentRow.IsNewRow)
            {
                try
                {
                    short shopId = (short)currentRow.Cells["IDColumn"].Value;
                    taobaodbEntities entities = ResourceManager.Current.LoadEntity();
                    var shop = (from s in entities.Shop
                                where s.ShopId == shopId
                                select s).FirstOrDefault();
                    if (shop != null)
                    {
                        shop.Name = ShopNameTextBox.Text;
                        shop.AppKey = AppKeyTextBox.Text;
                        shop.AppSecret = AppSecretTextBox.Text;
                        entities.SaveChanges();
                        BindGrid();
                    }
                }
                catch { }
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            DataGridViewRow currentRow = MainDataGridView.CurrentRow;
            if (currentRow != null && !currentRow.IsNewRow)
             {
                 try
                 {
                     short shopId = (short)currentRow.Cells["IDColumn"].Value;
                     taobaodbEntities entities = ResourceManager.Current.LoadEntity();
                     var shop = (from s in entities.Shop
                                 where s.ShopId == shopId
                                 select s).FirstOrDefault();
                     if (shop != null)
                     {
                         entities.DeleteObject(shop);
                         entities.SaveChanges();
                         BindGrid();
                     }
                 }
                 catch { }
             }
        }

        private void MainDataGridView_SelectionChanged(object sender, EventArgs e)
        {
             DataGridViewRow currentRow = MainDataGridView.CurrentRow; 
             if (currentRow != null && !currentRow.IsNewRow)
             {
                 ShopNameTextBox.Text = currentRow.Cells["NameColumn"].Value.ToString();
                 AppKeyTextBox.Text = currentRow.Cells["AppKeyColumn"].Value.ToString();
                 AppSecretTextBox.Text = currentRow.Cells["AppSecretColumn"].Value.ToString(); 
             }
        }

        #endregion

        #region Assit Methods

        protected void ClearForm()
        {
            ShopNameTextBox.Text = "";
            AppKeyTextBox.Text = "";
            AppSecretTextBox.Text = ""; 
        }

        protected Shop GetShop(short shopId)
        {
            try
            {
                taobaodbEntities entities = ResourceManager.Current.LoadEntity();
                return (from s in entities.Shop
                        where s.ShopId == shopId
                        select s).FirstOrDefault();
            }
            catch { }
            return null;
        }


        protected void BindGrid()
        {
            try
            {
                taobaodbEntities entities = ResourceManager.Current.LoadEntity();
                MainDataGridView.AutoGenerateColumns = false;
                MainDataGridView.DataSource = (from s in entities.Shop select s).ToArray();
            }
            catch { }
        }
        #endregion
    }
}
